﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Handsfree
{
    public class KeyboardOperation
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); 

        public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
        public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
        

        public void KeyPress(String s)
        {
            try
            {
                int VK_NUMLOCK = Convert.ToInt32(s, 16); //Mute
                // Simulate a key press
                keybd_event((byte)VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY, 0);
            }
            catch (Exception)
            {
                Console.WriteLine(s);
            }

        }
        public void KeyRelease(String s)
        {
            try
            {
                int VK_NUMLOCK = Convert.ToInt32(s, 16); //Mute
                // Simulate a key release
                keybd_event((byte)VK_NUMLOCK,
                             0x45,
                             KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
                             0);
            }
            catch (Exception)
            {
                Console.WriteLine(s);
            }
        }
    }
}
